<?php
use Swoole\Coroutine\Http\Client;
use Swoole\Process;

$conf = '';
include 'db.php';
include 'env/env.php';

Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_ALL);
define('MASTER_PID_FILE', __DIR__.'/runtime/'.GAME_NAME.'_'.APP_NAME.'_swoole_master.pid');
define('LOG_FILE', __DIR__.'/runtime/'.GAME_NAME.'_'.APP_NAME.'_swoole.log');

$process_count = 2;




//创建Server对象，监听 127.0.0.1:9501 端口。
$server = new Swoole\Server($conf['server']['host'], 9999);

for ($i = 0; $i < $process_count; $i++) {
    $process = new Swoole\Process(function (Swoole\Process $worker) use ($i) {
        // 子进程逻辑
        echo "Custom Process [$i] started, PID: " . $worker->pid . "\n";
        swoole_set_process_name('Process_' . $worker->pid); // 设置进程名

        while (true) {
            echo "Process [$i] is working...\n";
            sleep(5);
        }
    }, false, false);

    // ✅ 关键：在 start() 之前添加
    $server->addProcess($process);

    // 保存对象（用于后续管理，如信号处理）
    $custom_processes[] = $process;

    echo "Custom Process [$i] prepared.\n";
}

function process_name($name){
    $names = [GAME_NAME,APP_NAME,APP_ENV,$name];
    swoole_set_process_name(implode('_',$names));
}

function intMaster($conf){
    process_name('Manager');
}






$server->set([
    'worker_num' => WORKER_NUM,
    'enable_coroutine' => true,
    'reload_async' => true,
    'max_wait_time' => 60,
    'log_date_format' => '%Y-%m-%d %H:%M:%S',
    'display_errors' => true,
    'debug_mode' => APP_DEBUG,
    'daemonize' => APP_ENV=='production',
    'log_file' =>LOG_FILE,

]);
$process_count = 2;
intMaster($conf);
\swoole_process::signal(SIGTERM, function () use ($server) {
    echo "Received SIGTERM, shutting down...\n";
    $server->shutdown();
});
\swoole_process::signal(SIGUSR1, function () use ($server) {
    echo "Received SIGTERM, shutting down...\n";
    $server->reload();
});
$server->on('Connect', function ($server, $fd) {
    echo "Client: Connect.\n";
});

$server->on('Receive', function ($server, $fd, $reactor_id, $data) use($conf){

});

$server->on('Close', function ($server, $fd) {
    echo "Client: Close.\n";
});
$server->on('Start', function ($server) {
    echo "3. onStart: Master 进程启动\n";
    process_name('me_Master');
});
$server->on('ManagerStart', function ($server) use ($process_count){
    echo "2. onManagerStart: Manager 进程启动\n";
});

$server->on('WorkerStart', function ($server, $worker_id) {
    echo "4. onWorkerStart: Worker[$worker_id] 启动\n";
    process_name('me_Worker_' . $worker_id);
});

$server->on('ManagerStop', function ($server) {
    echo "7. onManagerStop: Manager 进程停止\n";
});

$server->on('WorkerStop', function ($server, $worker_id) {
    echo "5. onWorkerStop: Worker[$worker_id] 停止\n";
});

$server->on('WorkerExit', function ($server, $worker_id) {
    echo "6. onWorkerExit: Worker[$worker_id] 退出\n";
});

Swoole\Process::signal(SIGCHLD, function ($sig) {
    while ($ret = Swoole\Process::wait(false)) {
        if ($ret) {
            echo "Child Process exited, PID: {$ret['pid']}\n";
        }
    }
});

\swoole_process::signal(SIGTERM, function () use ($server) {
    echo "Received SIGTERM, shutting down...\n";
    $server->shutdown();
});
\swoole_process::signal(SIGUSR1, function () use ($server) {
    echo "Received SIGUSR1, shutting down...\n";
    $server->reload();
});

function clearTimer($server, $worker_id)
{
    if (isset($server->ticks[$worker_id])) {
        $server->clearTimer($server->ticks[$worker_id]);
    }
}

//启动服务器
$server->start();